<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="执行开始日期">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleClean"
              v-hasPermi="['kettle:record:clean']"
            >清空</el-button>
          </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['kettle:record:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['kettle:record:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

      <el-table border="border" max-height="550px" v-loading="loading" :data="recordList"
                @selection-change="handleSelectionChange"
                @sort-change="handleSortChange" :row-style="tableRowStyle" ref="record">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['kettle:record:edit']"
            >详情
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['kettle:record:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="分类名称" align="center" prop="jobName" width="150" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="执行类型" align="center" width="150" prop="ftype" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">{{ scope.row.ftype === 1 ? '作业任务' : scope.row.ftype === 2 ? '明细作业任务':""}}</template>
        </el-table-column>
        <el-table-column label="启动时间" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="停止时间" align="center" prop="stopTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.stopTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="转换日志" align="center" width="180" prop="logFile" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="执行时长" align="center" prop="duration" width="150" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="执行条数" align="center" prop="rowcount" width="150" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">{{ scope.row.status === 1 ? '运行成功' : scope.row.status === 2 ? '运行失败': "运行中"}}</template>
        </el-table-column>
        <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom"
                         :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom"
                         :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
      </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改日志信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="启动时间" prop="startTime">
                  <el-date-picker clearable
                                  v-model="form.startTime"
                                  type="datetime"
                                  value-format="yyyy-MM-dd HH:mm:ss"
                                  placeholder="请选择启动时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="停止时间" prop="stopTime">
                  <el-date-picker clearable
                                  v-model="form.stopTime"
                                  type="datetime"
                                  value-format="yyyy-MM-dd HH:mm:ss"
                                  placeholder="请选择停止时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="转换日志" prop="logFile">
                  <monaco-editor ref="monacoEditor"  style="border:1px solid #000;" :opts="opts"></monaco-editor>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="执行时长" v-if="false" prop="duration">
                  <el-input clearable v-model="form.duration" placeholder="请输入执行时长"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="执行条数" v-if="false" prop="rowcount">
                  <el-input clearable v-model="form.rowcount" placeholder="请输入执行条数"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" v-if="false" :loading="butloading" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { listRecord, getRecord, delRecord, addRecord, updateRecord,handleClean } from "@/api/kettle/record";
    import { getTableRowStyle } from "@/utils/yss";
    import monacoEditor from '@/components/SqlEditor';
    export default {
        name: "Record",
        components:{
          monacoEditor
        },
        dicts: ['sys_common_status'],
        data() {
            return {
              butloading:false,
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 日志信息表格数据
                recordList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 日期范围
                dateRange: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 50,
                    recordTransId: null,
                    startTime: null,
                    stopTime: null,
                    logFile: null,
                    jobId: null,
                    duration: null,
                    rowcount: null,
                    ftype: null,
                    status: null,
                    createTime: null,
                    createBy: null,
                    updateTime: null,
                    updateBy: null,
                    remark: null
                },
             opts: { //编辑器
                value: '',
                readOnly: false, // 是否可编辑
                language: 'sql', // 语言类型
                theme: 'vs', // 编辑器主题
                lineNumbers: "on", // 行号 取值
              },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询日志信息列表 */
            getList() {
                this.loading = true;
                listRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.recordList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                        id: null,
                        recordTransId: null,
                        startTime: null,
                        stopTime: null,
                        logFile: null,
                        jobId: null,
                        duration: null,
                        rowcount: null,
                        ftype: null,
                        status: null,
                        createTime: null,
                        createBy: null,
                        updateTime: null,
                        updateBy: null,
                        remark: null
            };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 排序触发事件 */
            handleSortChange(column, prop, order) {
                this.queryParams.orderByColumn = column.prop;
                this.queryParams.isAsc = column.order;
                this.getList();
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getRecord(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.opts.value= '';
                this.opts.value= response.data.logFile;
                this.title = "日志信息详情";
            });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                      this.butloading=true;
                        if (this.form.id != null) {
                            updateRecord(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        } else {
                            addRecord(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除数据项？').then(function() {
                    return delRecord(ids);
                }).then(() => {
                    this.getList();
                     this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
            },
          handleClean(){ /**清空日志*/
            this.$modal.confirm('确定清空日志？').then(function() {
              return handleClean();
            }).then(() => {
              this.getList();
              this.$modal.msgSuccess("清空成功");
          }).catch(() => {});
         },
    /** 导出按钮操作 */
    handleExport() {
        this.download('kettle/record/export', {
                ...this.queryParams
    }, `record_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    /** 单击选中行数据 */
    clickRow(row) {
        this.$refs.record.toggleRowSelection(row);
    }
    }
    };
</script>
